import { pluginVersion } from "../index";
const NERtcEvent = uni.requireNativePlugin('globalEvent');
const NERtcMoudle = uni.requireNativePlugin('NERTCUniPluginSDK-NERTCEngineImpl');

const Prefix = NERtcMoudle?.prefix();
let engine;
let listeners = new Map();

export class NERtcEngineImpl {
	getPrefix() {
		return Prefix;
	}
	
    static _callMethod(method, args) {
        return new Promise((resolve, _) => {
			console.log('NERtcEngineImpl _callMethod 开始: ', method, JSON.stringify(args))
            NERtcMoudle.callMethod({ method: method, args: args }, (res) => {
				console.log('NERtcEngineImpl _callMethod 完成: ', method, JSON.stringify(res))
                resolve(res);
            });
        });
    }
	
    static getInstance() {
        if (engine) {
            return engine;
        }
        throw new Error('Get instance failed, Please create engine first');
    }
	
    static setupEngineWithContext(options) {
        if (engine) {
            return engine;
        }
		console.log('NERtcEngineImpl: setupEngineWithContext() 创建nertc引擎')
		NERtcMoudle.nertcPrint('DCTestUniPlugin 创建nertc引擎: ' + JSON.stringify(options))
        NERtcMoudle.setupEngineWithContext(options);
		console.log('NERtcEngineImpl: setupEngineWithContext() done')
		NERtcMoudle.nertcPrint('DCTestUniPlugin 创建nertc引擎完成')
		engine = new NERtcEngineImpl();
        return engine;
    }
	
    destroyEngine() {
        engine = undefined;
		console.log('NERtcEngineImpl: destroyEngine() 销毁nertc引擎')
		NERtcMoudle.nertcPrint('销毁nertc引擎')
        NERtcMoudle.destroyEngine();
        listeners.forEach((_, key) => {
            NERtcEvent.removeEventListener(Prefix + key);
        });
        listeners.clear();
    }
	
	setParameters(options) {
		NERtcMoudle.setParameters(options)
	}
	
	destroyTCPServer() {
		NERtcMoudle.destroyTCPServer()
	}
	
	sendMessage(options) {
		NERtcMoudle.sendMessage(options)
	}
	
	nertcPrint(message) {
		NERtcMoudle.nertcPrint(message)
	}
	
    addEventListener(event, callback) {
        const native_listener = (res) => {
            const { data } = res;
            callback(...data);
        };
        let map = listeners.get(event);
        if (map === undefined) {
            map = new Map();
            listeners.set(event, map);
        }
        map.set(callback, native_listener);
        NERtcEvent.addEventListener(Prefix + event, native_listener);
    }
	
    removeEventListener(event, callback) {
        if (callback === undefined) {
            NERtcEvent.removeEventListener(Prefix + event);
            listeners.delete(event);
        }
        else {
            const map = listeners.get(event);
            if (map === undefined)
                return;
            NERtcEvent.removeEventListener(Prefix + event, map.get(callback));
            map.delete(callback);
        }
    }
	
	removeAllEventListener() {
		listeners.forEach((_, key) => {
		    NERtcEvent.removeEventListener(Prefix + key);
		});
		listeners.clear();
	}
	
	uploadSdkInfo() {
	    return NERtcMoudle.uploadSdkInfo();
	}
	getConnectionState() {
	    return NERtcMoudle.getConnectionState();
	}
	
	setStatsObserver(enable) {
	    return NERtcMoudle.setStatsObserver(enable);
	}
	
	setChannelProfile(channelProfile) {
	    return NERtcMoudle.setChannelProfile(channelProfile);
	}
	
	setClientRole(role) {
	    return NERtcMoudle.setClientRole(role);
	}
	
	joinChannel(options) {
	    return NERtcMoudle.joinChannel(options);
	}
	
	leaveChannel() {
	    return NERtcMoudle.leaveChannel();
	}
	
	setupLocalVideoCanvas(videoCanvasParam) {
	    return NERtcMoudle.setupLocalVideoCanvas(videoCanvasParam);
	}
	destroyLocalVideoCanvas() {
	    return NERtcMoudle.destroyLocalVideoCanvas();
	}
	
	async setupRemoteVideoCanvas(videoCanvasParam) {
		return NERtcMoudle.setupRemoteVideoCanvas(videoCanvasParam);
	}
	destroyRemoteVideoCanvas(options) {
	    return NERtcMoudle.destroyRemoteVideoCanvas(options);
	}
	async setupLocalSubStreamVideoCanvas(videoCanvasParam) {
	    return NERtcMoudle.setupLocalSubStreamVideoCanvas(videoCanvasParam);
	}
	destroyLocalSubStreamVideoCanvas() {
	    return NERtcMoudle.destroyLocalSubStreamVideoCanvas();
	}
	
	async setupRemoteSubStreamVideoCanvas(videoCanvasParam) {
	    return NERtcMoudle.setupRemoteSubStreamVideoCanvas(videoCanvasParam);
	}
	destroyRemoteSubStreamVideoCanvas(options) {
	    return NERtcMoudle.destroyRemoteSubStreamVideoCanvas(options);
	}
	
    startPreview(videoStreamType) {
        return NERtcMoudle.startPreview(videoStreamType);
    }
    stopPreview(videoStreamType) {
        return NERtcMoudle.stopPreview(videoStreamType);
    }

    setSpeakerphoneOn(enable) {
        return NERtcMoudle.setSpeakerphoneOn(enable);
    }
	
	enableMediaPub(enable){
		return NERtcMoudle.enableMediaPub(enable);
	}
	
	setRecordDeviceMute(enable){
		return NERtcMoudle.setRecordDeviceMute(enable);
	}
	
	adjustRecordingSignalVolume(volume){
		return NERtcMoudle.adjustRecordingSignalVolume(volume);
	}
	
	adjustUserPlaybackSignalVolume(options){
		return NERtcMoudle.adjustUserPlaybackSignalVolume(options);
	}
	
	adjustChannelPlaybackSignalVolume(volume){
		return NERtcMoudle.adjustChannelPlaybackSignalVolume(volume);
	}
	
	enableEarback(options){
		return NERtcMoudle.enableEarback(options);
	}
	
	setEarbackVolume(volume){
		return NERtcMoudle.setEarbackVolume(volume);
	}
	
	enableAudioVolumeIndication(options){
		return NERtcMoudle.enableAudioVolumeIndication(options);
	}
	
	enableLocalVideo(options) {
	    return NERtcMoudle.enableLocalVideo(options);
	}
	
	setAudioProfile(options) {
		return NERtcMoudle.setAudioProfile(options);
	}
	
	startScreenCapture(options) {
		return NERtcMoudle.startScreenCapture(options);
	}
	
	stopScreenCapture() {
		return NERtcMoudle.stopScreenCapture();
	}
	
	setLocalVideoConfig(options) {
		return NERtcMoudle.setLocalVideoConfig(options);
	}
	
	enableLocalAudio(enabel) {
	    return NERtcMoudle.enableLocalAudio(enabel);
	}
	
	muteLocalVideo(mute) {
	    return NERtcMoudle.muteLocalVideo(mute);
	}
	
	muteLocalAudio(mute) {
	    return NERtcMoudle.muteLocalAudio(mute);
	}
	
	switchCamera() {
	    return NERtcMoudle.switchCamera();
	}
	
	subscribeRemoteVideo(options) {
	    return NERtcMoudle.subscribeRemoteVideo(options);
	}

    subscribeRemoteSubStreamVideo(options) {
        return NERtcMoudle.subscribeRemoteSubStreamVideo(options);
    }
}
